<?php

namespace App\Http\Controllers\Web;

use Illuminate\Http\Request;
use App\Models\Orders;
use App\Models\Company;
use App\Models\Website;
use App\Models\Year;
use Illuminate\Support\Facades\Auth;

class AuthenticationController extends Controller {

    public function __construct() {

    }
    public function smrz() {
        //dd($_SERVER);
        return view('web.authentication.shiming');
    }

    public function qy(Request $request) {
        $uid = Auth::check() ? Auth::user()->id : null; // 获取的是用户id
        if ($request->ajax()) {
            $fullValue = $request->all(); //表单提交的所有内容
            $order = new Orders();
            $order->order_name = $fullValue['company_name'];
            $order->order_type = 1;
            $order->order_status = 0;
            if ($uid) {
                $order->uid = $uid;
            }
            $order->created_at = time();
            $order->sn = date("Ymdhis") . rand(10000, 99999);
            $order->phone = $fullValue['applicant_mobile']; //联系人电话
            $year_id = $fullValue['verify_year']; //根据
            $yid = Year::find($year_id);
            $total_money = $yid['year'] * $yid['money'];
            $order->year_remarks = "购买".$yid['year']."年，赠送".$yid['gift_time']."年，每年".$yid['money']."元";
            $order->money = $total_money;
            $resule = $order->save();  //保存
            if ($resule) {
                $company = new Company();
                $company->company_name = $fullValue['company_name']; //企业全称
                $company->organizer_province = $fullValue['organizer_province']; //省
                $company->organizer_city = $fullValue['organizer_city']; //市
                $company->company_type = $fullValue['company_type']; //企业类型
                $company->year = $fullValue['verify_year'] + $yid['gift_time']; //申请年限
                $company->contacts_name = $fullValue['applicant_name']; //联系人
                $company->contacts_emal = $fullValue['applicant_email']; //联系人邮箱
                $company->contacts_phone = $fullValue['applicant_mobile']; //联系人电话
                $company->created_at = time();
                $comres = $company->save();  //保存
                if (!empty($company['id'])) {
                    $data = array();
                    $post = Orders::find($order['id']);
                    $post->relation_id = $company['id'];
                    $post->updated_at = time();
                    if ($post->save()) {
                        echo 1;exit;
                    } else {
                        echo 0;exit;
                    }
                } else {

                    echo 0;exit;
                }
            } else {
                echo '数据提交失败，请重试！';exit;
            }
        }
        return view('web.authentication.qiye');
    }

    public function gw(Request $request) {
          $uid = Auth::check() ? Auth::user()->id : null; // 获取的是用户id
        if ($request->ajax()) {
            $fullValue = $request->all(); //表单提交的所有内容
            $order = new Orders();
            if ($uid) {
                $order->uid = $uid;
            }
            $order->order_name = $fullValue['verify_url'];
            $order->order_type = 0;
            $order->order_status = 0;
            $order->created_at = time();
            $order->sn = date("Ymdhis") . rand(10000, 99999);
            $order->phone = $fullValue['applicant_mobile']; //联系人电话
            $year_id = $fullValue['verify_year']; //根据
            $yid = Year::find($year_id);
            $total_money = $yid['year'] * $yid['money'];
            $order->money = $total_money;
            $order->year_remarks = "购买".$yid['year']."年，赠送".$yid['gift_time']."年，每年".$yid['money']."元";
            $resule = $order->save();  //保存
            if ($resule) {
                $website = new Website();
                $website->site_type_first = $fullValue['get_site_type_first']; //网站分类 1
                $website->site_type_second = $fullValue['get_site_type_second']; //网站分类 2
                $website->organizer_province = $fullValue['organizer_province']; //省
                $website->organizer_city = $fullValue['organizer_city']; //市
                $website->verify_url = $fullValue['verify_url']; //申请验证网站地址
                $website->year = $fullValue['verify_year'] + $yid['gift_time']; //申请年限
                $website->contacts_name = $fullValue['applicant_name']; //联系人
                $website->contacts_emal = $fullValue['applicant_email']; //联系人邮箱
                $website->contacts_phone = $fullValue['applicant_mobile']; //联系人电话
                $website->application_time = time();
                $website->created_at = time();
                $webres = $website->save();  //保存
                if (!empty($website['id'])) {
                    $data = array();
                    $post = Orders::find($order['id']);
                    $post->relation_id = $website['id'];
                    $post->updated_at = time();
                    if ($post->save()) {
                        echo 1;exit;
                    } else {
                        echo 0;exit;
                    }
                }
            }
        }
        return view('web.authentication.guanwang');
    }

    public function trial(Request $request) {
        if ($request->getMethod() == 'POST') {
            dd($request->all());
        }
        $Order_Status = Year::get();
        return view('web.authentication.trial', ['order_status' => $Order_Status]);
    }

    public function success() {
        return view('web.authentication.success');
    }

    //快速查询认证
    public function searchsite(Request $request) {
        $keyword = $request->get('search_key');
        $isMatched = preg_match('/^^www\d{0,3}[.]+[a-z0-9.\-]+[.][a-z]{2,4}|[a-z0-9.\-]+[.][a-z]{2,4}/', $keyword, $matches);
        if ($isMatched) {
            //查询website biao
            $webresult = Website::where(['verify_url' => $keyword, 'status' => 6])->first();
            if ($webresult) {
                return view('web.authentication.searchsite', ['webresult' => $webresult, 'keyword' => $keyword]);
            } else {
                return view('web.authentication.notfound');
            }
        } else {
            //查询company
            $webresult = Company::where(['company_name' => $keyword, 'status' => 6])->first();
            if ($webresult) {
                return view('web.authentication.searchcompany', ['webresult' => $webresult, 'keyword' => $keyword]);
            } else {
                return view('web.authentication.notfound');
            }
        }
    }

}
